package com.nit.generate;

import com.nit.tools.loop.NitLoopTools;

import java.util.Random;

/**
 * @ClassName NitEmailGenerator
 * @Description 邮箱地址生成器
 * @Author by
 * @Date 21/7/2023 上午9:52
 * @Version 1.0
 */
public class NitEmailGenerator {
    public static void main(String[] args) {
        NitLoopTools.loop(4, () -> {
            String tmp = NitEmailGenerator.generate();
            System.out.println("随机邮箱地址:" + tmp + "<br>");
        });
    }

    // 预定义的域名列表
    private static final String[] DOMAIN_LIST = {
            "gmail.com", "yahoo.com", "outlook.com", "hotmail.com", "example.com", "qq.com", "163.com"
    };

    // 生成随机用户名
    public static String generateRandomUsername() {
        String characters = "abcdefghijklmnopqrstuvwxyz0123456789";
        int usernameLength = 8; // 设置用户名长度为8个字符
        Random random = new Random();
        StringBuilder usernameBuilder = new StringBuilder();

        for (int i = 0; i < usernameLength; i++) {
            int index = random.nextInt(characters.length());
            char randomChar = characters.charAt(index);
            usernameBuilder.append(randomChar);
        }
        return usernameBuilder.toString();
    }

    // 生成随机邮箱地址
    public static String generate() {
        String username = generateRandomUsername();
        String domain = DOMAIN_LIST[new Random().nextInt(DOMAIN_LIST.length)];
        return username + "@" + domain;
    }

}
